package middleware

import "net/http"

func limitMiddleware(next http.Handler) http.Handler {
	var tokens = make(chan struct{}, 1)
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		select {
		case tokens <- struct{}{}:
			defer func() { <-tokens }()
			next.ServeHTTP(w, r)
		default:
			http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)
		}
	})
}
